home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Tool Chest / Development Tools & Languages / • Other Platforms / PCCTS 1.31 / testcpp / 5 / test.g < prev   
Encoding:
Text File  |  1995-03-10  |  1.2 KB  |  71 lines  |  [TEXT/MPS ]

  1. /* This is test.g which tests multiple scanners/parsers; DLG-based scanner */
  2. <<
  3. #include "Lexer.h"
  4. typedef ANTLRCommonToken ANTLRToken;
  5.  
  6. int main()
  7. {
  8.     ANTLRToken aToken;
  9.     DLGFileInput in(stdin);
  10.     Lexer scan(&in,2000);
  11.     scan.setToken(&aToken);
  12.     ANTLRTokenBuffer pipe(&scan);
  13.     Include parser(&pipe);
  14.     parser.init();
  15.  
  16.     parser.input();
  17.     return 0;
  18. }
  19. >>
  20.  
  21. #token "[\ \t\n]+"    <<skip();>>
  22. #token Eof "@"
  23.  
  24. class Include {
  25.  
  26. <<
  27. /* this is automatically defined to be a member function of Include::
  28.  * since it is within the "class {...}" boundaries.
  29.  */
  30. private:
  31. char *stripquotes(ANTLRChar *s)
  32. {
  33.     s[strlen(s)-1] = '\0';
  34.     return &s[1];
  35. }
  36. >>
  37.  
  38. input
  39.     :    ( cmd | include )* Eof
  40.     ;
  41.  
  42. cmd    :    "print"
  43.         (    NUMBER        <<printf("%s\n", $1->getText());>>
  44.         |    STRING        <<printf("%s\n", $1->getText());>>
  45.         )
  46.     ;
  47.  
  48. include
  49.     :    "#include" STRING
  50.         <<{
  51.         FILE *f;
  52.         f = fopen(stripquotes($2->getText()), "r");
  53.         if ( f==NULL ) {fprintf(stderr, "can't open %s\n", $2->getText()+1);}
  54.         else {
  55.             ANTLRToken aToken;
  56.             DLGFileInput in(f);
  57.             Lexer scan(&in,2000);
  58.             scan.setToken(&aToken);
  59.             ANTLRTokenBuffer pipe(&scan);
  60.             Include parser(&pipe);
  61.             parser.init();
  62.             parser.input();
  63.         }
  64.         }>>
  65.     ;
  66.  
  67. }
  68.  
  69. #token STRING    "\" [a-zA-Z0-9_.,\ \t]+ \""
  70. #token NUMBER    "[0-9]+"
  71.